package utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.part;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.gmf.runtime.notation.View;

import utn.frsf.cidisi.upcolbpip.interactionprotocols.BusinessMessage;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.ControlFlowSegment;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionPath;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionProtocol;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionProtocolElement;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionprotocolsPackage;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.Lifeline;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.Role;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.BusinessMessageEditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.ControlFlowSegment2EditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.ControlFlowSegmentControlFlowSegmentCompartment2EditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.ControlFlowSegmentControlFlowSegmentCompartmentEditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.ControlFlowSegmentEditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.InteractionPathEditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.InteractionPathInteractionPathCompartmentEditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.InteractionProtocolEditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.Lifeline2EditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.ProtocolReference2EditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.ProtocolReferenceEditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.RoleEditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.RoleRoleCompartmentEditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.Termination2EditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.TerminationEditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.providers.UpcolbpipElementTypes;

/**
 * @generated
 */
public class UpcolbpipDiagramUpdater {

	/**
	 * @generated
	 */
	public static List getSemanticChildren(View view) {
		switch (UpcolbpipVisualIDRegistry.getVisualID(view)) {
		case RoleRoleCompartmentEditPart.VISUAL_ID:
			return getRoleRoleCompartment_7004SemanticChildren(view);
		case ControlFlowSegmentControlFlowSegmentCompartmentEditPart.VISUAL_ID:
			return getControlFlowSegmentControlFlowSegmentCompartment_7001SemanticChildren(view);
		case InteractionPathInteractionPathCompartmentEditPart.VISUAL_ID:
			return getInteractionPathInteractionPathCompartment_7002SemanticChildren(view);
		case ControlFlowSegmentControlFlowSegmentCompartment2EditPart.VISUAL_ID:
			return getControlFlowSegmentControlFlowSegmentCompartment_7003SemanticChildren(view);
		case InteractionProtocolEditPart.VISUAL_ID:
			return getInteractionProtocol_1100SemanticChildren(view);
		}
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getRoleRoleCompartment_7004SemanticChildren(View view) {
		if (false == view.eContainer() instanceof View) {
			return Collections.EMPTY_LIST;
		}
		View containerView = (View) view.eContainer();
		if (!containerView.isSetElement()) {
			return Collections.EMPTY_LIST;
		}
		Role modelElement = (Role) containerView.getElement();
		List result = new LinkedList();
		{
			Lifeline childElement = modelElement.getRoleLifeline();
			int visualID = UpcolbpipVisualIDRegistry.getNodeVisualID(view,
					childElement);
			if (visualID == Lifeline2EditPart.VISUAL_ID) {
				result.add(new UpcolbpipNodeDescriptor(childElement, visualID));
			}
		}
		return result;
	}

	/**
	 * @generated
	 */
	public static List getControlFlowSegmentControlFlowSegmentCompartment_7001SemanticChildren(
			View view) {
		if (false == view.eContainer() instanceof View) {
			return Collections.EMPTY_LIST;
		}
		View containerView = (View) view.eContainer();
		if (!containerView.isSetElement()) {
			return Collections.EMPTY_LIST;
		}
		ControlFlowSegment modelElement = (ControlFlowSegment) containerView
				.getElement();
		List result = new LinkedList();
		for (Iterator it = modelElement.getInteractionPath().iterator(); it
				.hasNext();) {
			InteractionPath childElement = (InteractionPath) it.next();
			int visualID = UpcolbpipVisualIDRegistry.getNodeVisualID(view,
					childElement);
			if (visualID == InteractionPathEditPart.VISUAL_ID) {
				result.add(new UpcolbpipNodeDescriptor(childElement, visualID));
				continue;
			}
		}
		return result;
	}

	/**
	 * @generated
	 */
	public static List getInteractionPathInteractionPathCompartment_7002SemanticChildren(
			View view) {
		if (false == view.eContainer() instanceof View) {
			return Collections.EMPTY_LIST;
		}
		View containerView = (View) view.eContainer();
		if (!containerView.isSetElement()) {
			return Collections.EMPTY_LIST;
		}
		InteractionPath modelElement = (InteractionPath) containerView
				.getElement();
		List result = new LinkedList();
		for (Iterator it = modelElement.getElement().iterator(); it.hasNext();) {
			InteractionProtocolElement childElement = (InteractionProtocolElement) it
					.next();
			int visualID = UpcolbpipVisualIDRegistry.getNodeVisualID(view,
					childElement);
			if (visualID == ControlFlowSegment2EditPart.VISUAL_ID) {
				result.add(new UpcolbpipNodeDescriptor(childElement, visualID));
				continue;
			}
			if (visualID == ProtocolReference2EditPart.VISUAL_ID) {
				result.add(new UpcolbpipNodeDescriptor(childElement, visualID));
				continue;
			}
			if (visualID == Termination2EditPart.VISUAL_ID) {
				result.add(new UpcolbpipNodeDescriptor(childElement, visualID));
				continue;
			}
		}
		return result;
	}

	/**
	 * @generated
	 */
	public static List getControlFlowSegmentControlFlowSegmentCompartment_7003SemanticChildren(
			View view) {
		if (false == view.eContainer() instanceof View) {
			return Collections.EMPTY_LIST;
		}
		View containerView = (View) view.eContainer();
		if (!containerView.isSetElement()) {
			return Collections.EMPTY_LIST;
		}
		ControlFlowSegment modelElement = (ControlFlowSegment) containerView
				.getElement();
		List result = new LinkedList();
		for (Iterator it = modelElement.getInteractionPath().iterator(); it
				.hasNext();) {
			InteractionPath childElement = (InteractionPath) it.next();
			int visualID = UpcolbpipVisualIDRegistry.getNodeVisualID(view,
					childElement);
			if (visualID == InteractionPathEditPart.VISUAL_ID) {
				result.add(new UpcolbpipNodeDescriptor(childElement, visualID));
				continue;
			}
		}
		return result;
	}

	/**
	 * @generated
	 */
	public static List getInteractionProtocol_1100SemanticChildren(View view) {
		if (!view.isSetElement()) {
			return Collections.EMPTY_LIST;
		}
		InteractionProtocol modelElement = (InteractionProtocol) view
				.getElement();
		List result = new LinkedList();
		for (Iterator it = modelElement.getRole().iterator(); it.hasNext();) {
			Role childElement = (Role) it.next();
			int visualID = UpcolbpipVisualIDRegistry.getNodeVisualID(view,
					childElement);
			if (visualID == RoleEditPart.VISUAL_ID) {
				result.add(new UpcolbpipNodeDescriptor(childElement, visualID));
				continue;
			}
		}
		for (Iterator it = modelElement.getIpElement().iterator(); it.hasNext();) {
			InteractionProtocolElement childElement = (InteractionProtocolElement) it
					.next();
			int visualID = UpcolbpipVisualIDRegistry.getNodeVisualID(view,
					childElement);
			if (visualID == TerminationEditPart.VISUAL_ID) {
				result.add(new UpcolbpipNodeDescriptor(childElement, visualID));
				continue;
			}
			if (visualID == ProtocolReferenceEditPart.VISUAL_ID) {
				result.add(new UpcolbpipNodeDescriptor(childElement, visualID));
				continue;
			}
			if (visualID == ControlFlowSegmentEditPart.VISUAL_ID) {
				result.add(new UpcolbpipNodeDescriptor(childElement, visualID));
				continue;
			}
		}
		return result;
	}

	/**
	 * @generated
	 */
	public static List getContainedLinks(View view) {
		switch (UpcolbpipVisualIDRegistry.getVisualID(view)) {
		case InteractionProtocolEditPart.VISUAL_ID:
			return getInteractionProtocol_1100ContainedLinks(view);
		case RoleEditPart.VISUAL_ID:
			return getRole_2005ContainedLinks(view);
		case TerminationEditPart.VISUAL_ID:
			return getTermination_2003ContainedLinks(view);
		case ProtocolReferenceEditPart.VISUAL_ID:
			return getProtocolReference_2004ContainedLinks(view);
		case ControlFlowSegmentEditPart.VISUAL_ID:
			return getControlFlowSegment_2002ContainedLinks(view);
		case Lifeline2EditPart.VISUAL_ID:
			return getLifeline_3006ContainedLinks(view);
		case InteractionPathEditPart.VISUAL_ID:
			return getInteractionPath_3001ContainedLinks(view);
		case ControlFlowSegment2EditPart.VISUAL_ID:
			return getControlFlowSegment_3002ContainedLinks(view);
		case ProtocolReference2EditPart.VISUAL_ID:
			return getProtocolReference_3003ContainedLinks(view);
		case Termination2EditPart.VISUAL_ID:
			return getTermination_3004ContainedLinks(view);
		case BusinessMessageEditPart.VISUAL_ID:
			return getBusinessMessage_4001ContainedLinks(view);
		}
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getIncomingLinks(View view) {
		switch (UpcolbpipVisualIDRegistry.getVisualID(view)) {
		case RoleEditPart.VISUAL_ID:
			return getRole_2005IncomingLinks(view);
		case TerminationEditPart.VISUAL_ID:
			return getTermination_2003IncomingLinks(view);
		case ProtocolReferenceEditPart.VISUAL_ID:
			return getProtocolReference_2004IncomingLinks(view);
		case ControlFlowSegmentEditPart.VISUAL_ID:
			return getControlFlowSegment_2002IncomingLinks(view);
		case Lifeline2EditPart.VISUAL_ID:
			return getLifeline_3006IncomingLinks(view);
		case InteractionPathEditPart.VISUAL_ID:
			return getInteractionPath_3001IncomingLinks(view);
		case ControlFlowSegment2EditPart.VISUAL_ID:
			return getControlFlowSegment_3002IncomingLinks(view);
		case ProtocolReference2EditPart.VISUAL_ID:
			return getProtocolReference_3003IncomingLinks(view);
		case Termination2EditPart.VISUAL_ID:
			return getTermination_3004IncomingLinks(view);
		case BusinessMessageEditPart.VISUAL_ID:
			return getBusinessMessage_4001IncomingLinks(view);
		}
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getOutgoingLinks(View view) {
		switch (UpcolbpipVisualIDRegistry.getVisualID(view)) {
		case RoleEditPart.VISUAL_ID:
			return getRole_2005OutgoingLinks(view);
		case TerminationEditPart.VISUAL_ID:
			return getTermination_2003OutgoingLinks(view);
		case ProtocolReferenceEditPart.VISUAL_ID:
			return getProtocolReference_2004OutgoingLinks(view);
		case ControlFlowSegmentEditPart.VISUAL_ID:
			return getControlFlowSegment_2002OutgoingLinks(view);
		case Lifeline2EditPart.VISUAL_ID:
			return getLifeline_3006OutgoingLinks(view);
		case InteractionPathEditPart.VISUAL_ID:
			return getInteractionPath_3001OutgoingLinks(view);
		case ControlFlowSegment2EditPart.VISUAL_ID:
			return getControlFlowSegment_3002OutgoingLinks(view);
		case ProtocolReference2EditPart.VISUAL_ID:
			return getProtocolReference_3003OutgoingLinks(view);
		case Termination2EditPart.VISUAL_ID:
			return getTermination_3004OutgoingLinks(view);
		case BusinessMessageEditPart.VISUAL_ID:
			return getBusinessMessage_4001OutgoingLinks(view);
		}
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getInteractionProtocol_1100ContainedLinks(View view) {
		InteractionProtocol modelElement = (InteractionProtocol) view
				.getElement();
		List result = new LinkedList();
		result
				.addAll(getContainedTypeModelFacetLinks_BusinessMessage_4001(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List getRole_2005ContainedLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getTermination_2003ContainedLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getProtocolReference_2004ContainedLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getControlFlowSegment_2002ContainedLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getLifeline_3006ContainedLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getInteractionPath_3001ContainedLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getControlFlowSegment_3002ContainedLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getProtocolReference_3003ContainedLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getTermination_3004ContainedLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getBusinessMessage_4001ContainedLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getRole_2005IncomingLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getTermination_2003IncomingLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getProtocolReference_2004IncomingLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getControlFlowSegment_2002IncomingLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getLifeline_3006IncomingLinks(View view) {
		Lifeline modelElement = (Lifeline) view.getElement();
		Map crossReferences = EcoreUtil.CrossReferencer.find(view.eResource()
				.getResourceSet().getResources());
		List result = new LinkedList();
		result.addAll(getIncomingTypeModelFacetLinks_BusinessMessage_4001(
				modelElement, crossReferences));
		return result;
	}

	/**
	 * @generated
	 */
	public static List getInteractionPath_3001IncomingLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getControlFlowSegment_3002IncomingLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getProtocolReference_3003IncomingLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getTermination_3004IncomingLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getBusinessMessage_4001IncomingLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getRole_2005OutgoingLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getTermination_2003OutgoingLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getProtocolReference_2004OutgoingLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getControlFlowSegment_2002OutgoingLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getLifeline_3006OutgoingLinks(View view) {
		Lifeline modelElement = (Lifeline) view.getElement();
		List result = new LinkedList();
		result
				.addAll(getOutgoingTypeModelFacetLinks_BusinessMessage_4001(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List getInteractionPath_3001OutgoingLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getControlFlowSegment_3002OutgoingLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getProtocolReference_3003OutgoingLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getTermination_3004OutgoingLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getBusinessMessage_4001OutgoingLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	private static Collection getContainedTypeModelFacetLinks_BusinessMessage_4001(
			InteractionProtocol container) {
		Collection result = new LinkedList();
		for (Iterator links = container.getIpElement().iterator(); links
				.hasNext();) {
			Object linkObject = links.next();
			if (false == linkObject instanceof BusinessMessage) {
				continue;
			}
			BusinessMessage link = (BusinessMessage) linkObject;
			if (BusinessMessageEditPart.VISUAL_ID != UpcolbpipVisualIDRegistry
					.getLinkWithClassVisualID(link)) {
				continue;
			}
			Lifeline dst = link.getReceiver();
			Lifeline src = link.getSender();
			result.add(new UpcolbpipLinkDescriptor(src, dst, link,
					UpcolbpipElementTypes.BusinessMessage_4001,
					BusinessMessageEditPart.VISUAL_ID));
		}
		return result;
	}

	/**
	 * @generated
	 */
	private static Collection getIncomingTypeModelFacetLinks_BusinessMessage_4001(
			Lifeline target, Map crossReferences) {
		Collection result = new LinkedList();
		Collection settings = (Collection) crossReferences.get(target);
		for (Iterator it = settings.iterator(); it.hasNext();) {
			EStructuralFeature.Setting setting = (EStructuralFeature.Setting) it
					.next();
			if (setting.getEStructuralFeature() != InteractionprotocolsPackage.eINSTANCE
					.getBusinessMessage_Receiver()
					|| false == setting.getEObject() instanceof BusinessMessage) {
				continue;
			}
			BusinessMessage link = (BusinessMessage) setting.getEObject();
			if (BusinessMessageEditPart.VISUAL_ID != UpcolbpipVisualIDRegistry
					.getLinkWithClassVisualID(link)) {
				continue;
			}
			Lifeline src = link.getSender();
			result.add(new UpcolbpipLinkDescriptor(src, target, link,
					UpcolbpipElementTypes.BusinessMessage_4001,
					BusinessMessageEditPart.VISUAL_ID));
		}
		return result;
	}

	/**
	 * @generated
	 */
	private static Collection getOutgoingTypeModelFacetLinks_BusinessMessage_4001(
			Lifeline source) {
		InteractionProtocol container = null;
		// Find container element for the link.
		// Climb up by containment hierarchy starting from the source
		// and return the first element that is instance of the container class.
		for (EObject element = source; element != null && container == null; element = element
				.eContainer()) {
			if (element instanceof InteractionProtocol) {
				container = (InteractionProtocol) element;
			}
		}
		if (container == null) {
			return Collections.EMPTY_LIST;
		}
		Collection result = new LinkedList();
		for (Iterator links = container.getIpElement().iterator(); links
				.hasNext();) {
			Object linkObject = links.next();
			if (false == linkObject instanceof BusinessMessage) {
				continue;
			}
			BusinessMessage link = (BusinessMessage) linkObject;
			if (BusinessMessageEditPart.VISUAL_ID != UpcolbpipVisualIDRegistry
					.getLinkWithClassVisualID(link)) {
				continue;
			}
			Lifeline dst = link.getReceiver();
			Lifeline src = link.getSender();
			if (src != source) {
				continue;
			}
			result.add(new UpcolbpipLinkDescriptor(src, dst, link,
					UpcolbpipElementTypes.BusinessMessage_4001,
					BusinessMessageEditPart.VISUAL_ID));
		}
		return result;
	}

}
